package com.xhtranslate.service.ali.serviceImpl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.alimt20181012.Client;
import com.aliyun.alimt20181012.models.TranslateGeneralRequest;
import com.aliyun.alimt20181012.models.TranslateGeneralResponse;
import com.aliyun.teautil.models.RuntimeOptions;
import com.xhtranslate.common.config.TranslateConfig;
import com.xhtranslate.common.dto.ImageFormDTO;
import com.xhtranslate.common.dto.TextFormDTO;
import com.xhtranslate.common.util.AddUseLimitThreadUtil;
import com.xhtranslate.common.util.JudgeCreditLimitUtil;
import com.xhtranslate.common.vo.ResponseEnum;
import com.xhtranslate.service.ali.service.AliOcrService;
import com.xhtranslate.service.ali.service.AliTranslateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.regex.Pattern;

/**
 * @author liuxuhuang
 * @version 1.0
 * @package com.xhtranslate.service.baidu.serviceImpl
 * @date 2022/11/2 21:06:51
 * @description
 */
@Service
@Slf4j
public class AliTranslateImpl implements AliTranslateService {
    @Autowired
    @Qualifier(value = "textTranslateBean")
    private Client aliTextTranslateClient;
    @Autowired
    private AliOcrService aliOcrService;
    @Autowired
    private JudgeCreditLimitUtil judgeCreditLimitUtil;
    @Autowired
    private AddUseLimitThreadUtil addUseLimitThreadUtil;
    /**
     * 特殊字符匹配
     */
    private Pattern stringPattern = Pattern.compile(TranslateConfig.SpecialString);

    @Override
    public String commonTranslate(TextFormDTO dto) throws Exception {
        log.info("阿里文本翻译前=>内容=>" + dto.getContext());
        TranslateGeneralRequest translateGeneralRequest = new TranslateGeneralRequest()
                .setFormatType("text")
                .setSourceLanguage(dto.getFrom())
                .setTargetLanguage(dto.getTo())
                .setSourceText(dto.getContext())
                .setScene("general");
        RuntimeOptions options = new RuntimeOptions();
        TranslateGeneralResponse response = aliTextTranslateClient.translateGeneralWithOptions(translateGeneralRequest, options);
        JSONObject jsonObject = JSONObject.parseObject(String.valueOf(JSON.toJSON(response)));
        JSONObject jsonBody = jsonObject.getJSONObject("body");
        Integer code = jsonBody.getInteger("code");
        if (code != 200) {
            return "";
        }
        JSONObject dataObject = jsonBody.getJSONObject("data");
        String translated = dataObject.getString("translated");
        addUseLimitThreadUtil.addCreditLimit(TranslateConfig.AliTextTranslateRedisName, translated.length());
        log.info("阿里文本翻译后=>内容=>");
        log.info(translated);
        return translated;
    }

    @Override
    public String imageTranslate(ImageFormDTO dto) throws Exception {
        String ocrImgString = aliOcrService.ocrImg(dto.getFile());
        log.info("阿里图片翻译前：" + ocrImgString);
        if ("".equals(ocrImgString) || StringUtils.isBlank(ocrImgString)) {
            return "";
        }
        boolean b = judgeCreditLimitUtil.getBeyondTextCredit(TranslateConfig.AliTextTranslateRedisName, TranslateConfig.AliTextTranslateRedisCreditLimit, ocrImgString.length());
        if (!b) {
            return ResponseEnum.OVERCREDITLIMIT.getMsg();
        }
        TextFormDTO textFormDTO = new TextFormDTO(dto.getPlatform(), dto.getFrom(), dto.getTo(), ocrImgString);
        String translate = commonTranslate(textFormDTO);
        log.info("阿里图片翻译后：" + translate);
        return translate;
    }
}
